package sheshen.come.cn.sheshen.base.adpter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;


import java.util.ArrayList;
import java.util.List;

/**
 * 简单的adapter的封装
 * Created by hjb on 2015/12/31.
 */
public abstract class MyBaseAdapter<T> extends BaseAdapter {
    public Context context;

    public List<T> data=new ArrayList<>();

    public MyBaseAdapter(Context context, List<T> data) {
        super();
        this.context = context;
        if (data != null) {
            this.data = data;
        }
    }

    public void clear() {
        if (data != null) {
            this.data.clear();
        }
    }

    public void append(List<T> list) {
        if (this.data != null) {
            this.data.addAll(list);
        } else {
            this.data = list;
        }
        notifyDataSetChanged();
    }
    /**
     * 单个更新
     *
     * @param mode
     */
    public void updateSingleRow(ListView listView, T mode) {

        if (listView != null) {
            int start = listView.getFirstVisiblePosition();

            for (int i = start, j = listView.getLastVisiblePosition(); i <= j; i++)
                if (mode == listView.getItemAtPosition(i)) {

                    // 如果是listview
					/*
					 * View view = listView.getChildAt(i - start); getView(i,
					 * view, listView);
					 */
                    // xlistview 需要多减去1
                    View view = listView.getChildAt(i - start);
                    getView(i - 1, view, listView);
                    break;
                }
        }
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public abstract View getView(int position, View view, ViewGroup viewGroup);
}
